
//
//  DiscoverBusinessCommentsViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/14.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "DiscoverBusinessCommentsViewController.h"
#import "RDVTabBarController.h"
#import "RepairCell.h"
#import "Repairmodel.h"
#import "UIImageView+WebCache.h"

#import "HttpRequest.h"
#import "TheLoginViewController.h"
#import "HAODEsingleton.h"
#import "addRepairViewController.h"
#import "imageview.h"
#import "addRepairViewController.h"
#import "UIButton+WebCache.h"
#import "MCircleCell.h"
#import "HcCustomKeyboard.h"
@interface DiscoverBusinessCommentsViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,HcCustomKeyboardDelegate>

@property (nonatomic, strong) UIImageView * allImageBtn;
@property(nonatomic,strong)NSString*sta;
@end

@implementation DiscoverBusinessCommentsViewController{
    int index;
    NSString * hasNext;
    NSString *nextIndex;
    NSMutableArray *allComment;
    NSMutableArray *amt;
    NSMutableArray *detail;
    NSMutableArray *provinceName;
    NSMutableArray *cityName;
    NSMutableArray *status;
    NSMutableArray*imageUrlFull;
    NSMutableArray *time;
    NSMutableArray *reqId;
    NSMutableArray *address;
    NSMutableArray *userName;
    HAODEsingleton*commdity;
    UIButton*sat;
    UIButton*sat1;
    UIButton*sxuan;
    int tapImageCount;
    UIScrollView*MYmainScroll;
    NSMutableArray*arr;
    imageview*imageView;
    UIView*view;
    UIButton*i22;
    UIButton*i1;
    UIButton*i12;
    NSMutableArray *cDate;
    NSMutableArray *content;
    NSMutableArray *headImage;
    UITextView*tv;
    UIImageView*imagess;
    UILabel*label;
    
}
@synthesize alt = alt;
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [self setup];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";
    commdity=[HAODEsingleton shared];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    
    
}

- (void)setup{
    
    _table.delegate = self;
    _table.dataSource = self;
    
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_table];
    content=[[NSMutableArray alloc]init];
    userName=[[NSMutableArray alloc]init];
    headImage=[[NSMutableArray alloc]init];
    cDate=[[NSMutableArray alloc]init];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:commdity.reqId forKey:@"infoId"];
    
    NSLog(@"-----%@",params);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    [HttpRequest requestData:@"/Discover/Business/Comments" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                arr=[[severDataDict.dataDict objectForKey:@"mcircle"]objectForKey:@"images"];
                
                
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                UIButton*sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  180, 60/2)];
                [sxBtn setTitle:[NSString stringWithFormat:@"全部评论 %ld 条",  arrr.count] forState:UIControlStateNormal];
                [sxBtn setTitleColor:zhonghuiColor forState:UIControlStateNormal];
                sxBtn .titleLabel.font=[UIFont systemFontOfSize:15];
                sxBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
                UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
                self.navigationItem.rightBarButtonItem = myBtnBtn;
                
                for (NSDictionary* dicc in arrr )
                {
                    
                    [userName addObject:[dicc valueForKey:@"creatorName"]];
                    [content addObject:[dicc valueForKey:@"content"]];
                    
                    [headImage addObject:[dicc valueForKey:@"headImage"]];
                    
                    NSString* string =  [dicc valueForKey:@"createTime"];
                    NSLog(@"-------------%@",string);
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
                    NSDate *destDate= [dateFormatter dateFromString:string];
                    
                    NSTimeInterval  timeInterval = [destDate timeIntervalSinceNow];
                    timeInterval = -timeInterval;
                    
                    NSString *result;
                    
                    long temp = 0;
                    
                    if (timeInterval < 60) {
                        result = [NSString stringWithFormat:@"刚刚"];
                    }
                    else if((temp = timeInterval/60) <60){
                        result = [NSString stringWithFormat:@"%ld分前",temp];
                    }
                    
                    else if((temp = temp/60) <24){
                        result = [NSString stringWithFormat:@"%ld小时前",temp];
                    }
                    
                    else if((temp = temp/24) <30){
                        result = [NSString stringWithFormat:@"%ld天前",temp];
                    }
                    
                    else if((temp = temp/30) <12){
                        result = [NSString stringWithFormat:@"%ld月前",temp];
                    }
                    else{
                        temp = temp/12;
                        result = [NSString stringWithFormat:@"%ld年前",temp];
                    }
                    
                    
                    [cDate addObject:result];
                }
                
                
                [_table reloadData];
                
            }else{
                
                
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
                
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    TheLoginViewController*detailViewController = [[TheLoginViewController alloc] init];
                    
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
    
}
-(void)talkBtnClick:(UITextView *)textViewGet
{
    NSLog(@"%@",textViewGet.text);
    
    
    if (textViewGet.text.length==0)
    {
        
        [self showAlertMessage:@"内容为空"];
    }else{
        if ([textViewGet.text isEqualToString:@"我来说两句..."])
        {
            
            [self showAlertMessage:@"内容为空"];
        }else{
            
            
            NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
            NSString* tokenId = [user1 objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:tokenId forKey:@"tokenId"];
            [params setValue:commdity.reqId forKey:@"mcId"];
            [params setValue:textViewGet.text forKey:@"content"];
            NSLog(@"-----%@",params);
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:10 view:self.view];
            [HttpRequest requestData:@"/Discover/MCircle/AddComment" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    [Global hideProgressHUDInternal:self.view];
                    NSLog(@"-----%@",severDataDict.dataDict);
                    textViewGet.text=@"";
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        
                        
                        [self setup];
                        
                    }else{
                        
                        
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                        
                    }
                    
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
                
            }];
            
            
            
        }
        
    }
    
    
    
    
}

-(void)guangzhuBtnClick1:(UIButton *)guanzhu
{
    NSLog(@"%@",guanzhu.titleLabel.text);
    
    
    
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:commdity.reqId forKey:@"mcId"];
    
    NSLog(@"-----%@",params);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:10 view:self.view];
    [HttpRequest requestData:@"/Discover/MCircle/Praise" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            [Global hideProgressHUDInternal:self.view];
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                
                [[HcCustomKeyboard customKeyboard]textViewShowView:self String1:[severDataDict.dataDict objectForKey:@"isPraise"]    customKeyboardDelegate:self];
                
            }else{
                
                
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
                
                
            }
            
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
    
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [userName count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    MCircleCell* cell = (MCircleCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"MCircleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    
    //    cell.accessoryView = jiantou;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    cell.image.layer.masksToBounds = YES;
    cell.image.layer.cornerRadius = 40/2;
    [cell.image setImageWithURL:[NSURL URLWithString:[headImage objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@""]];
    
    
    
    cell.name.text =[NSString stringWithFormat:@"%@",  [userName objectAtIndex:indexPath.row]];
    NSString *s2 =[content objectAtIndex:indexPath.row];
    UIFont *font2 = [UIFont systemFontOfSize:15];
    CGSize size2 = CGSizeMake(ScreenWidth-40, 2000);
    CGSize labelsize2 = [s2 sizeWithFont:font2 constrainedToSize:size2 lineBreakMode:NSLineBreakByCharWrapping];
    
    commdity.Education=[NSString stringWithFormat:@"%f", labelsize2.height+20];
    cell.content.frame=CGRectMake(60, 50, ScreenWidth-40,labelsize2.height+20);
    cell.content.text =[NSString stringWithFormat:@"%@",  [content objectAtIndex:indexPath.row]];
    
    
    cell.time.text =[NSString stringWithFormat:@"%@",[cDate objectAtIndex:indexPath.row]];
    
    if (userName.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, commdity.Education.intValue+150/3-0.35, ScreenWidth-30, 0.35)];
        lineLabel1.backgroundColor=qianhuicolor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (userName.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, commdity.Education.intValue+150/3-0.35, ScreenWidth, 0.35)];
        lineLabel1.backgroundColor=qianhuicolor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return commdity.Education.intValue+150/3;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
